package form;

import database.DBO;
import widget.DataTable;

import javax.swing.*;
import java.awt.*;
import java.sql.ResultSet;

/**
 * Created by LLAP on 2017/12/28.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class Match extends Form {
    private DataTable offer, offerResult, demand, demandResult;
    private JPanel panelCondition, panelOffer, panelDemand, panelResult;
    private JButton offerToDemand, demandToOffer;
    private DBO db;
    public Match() {
        super("房源/客户匹配", 1000, 600);
        db = new DBO();
        panelCondition = new JPanel(new GridLayout(1, 2 ,5, 5));
        panelOffer = new JPanel(new BorderLayout());
        panelDemand = new JPanel(new BorderLayout());
        panelResult = new JPanel(new GridLayout(1, 2, 5, 5));
        offerToDemand = new JButton("匹配目标客户 -->");
        demandToOffer = new JButton("<-- 匹配目标房源");
        offer = new DataTable(new entity.Offer(), false);
        demand = new DataTable(new entity.Demand(), false);
        offerResult = new DataTable();
        demandResult = new DataTable();

        panelOffer.add("North", offerToDemand);
        panelOffer.add("Center", offer);
        panelDemand.add("North", demandToOffer);
        panelDemand.add("Center", demand);
        panelCondition.add(panelOffer);
        panelCondition.add(panelDemand);
        panelResult.add(offerResult);
        panelResult.add(demandResult);

        this.setLayout(new GridLayout(2, 1, 5, 5));
        this.add(panelCondition);
        this.add(panelResult);

        offerToDemand.addActionListener(e -> {
            try {
                Object[] data = offer.getSelectedRow();
                if (data.length == 0) return;
                ResultSet result = db.query(
                        "select * from demand where demand like ? and area <= ? and price >= ? " +
                                "and did in (select distinct did from demand_type where type like ?) " +
                                "and did in (select distinct did from demand_decor where decor like ?) " +
                                "and did in (select distinct did from demand_direction where direction like ?)",
                        new Object[] {
                                (data[1].equals("出租")?"求租":"求购"),
                                data[5], data[9], data[6], data[7], data[8]
                        }
                );
                panelResult.remove(demandResult);
                panelResult.remove(offerResult);
                demandResult = new DataTable(new entity.Demand(result));
                panelResult.add(offerResult);
                panelResult.add(demandResult);
                panelResult.updateUI();
            }
            catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        });
        demandToOffer.addActionListener(e -> {
            try {
                Object[] data = demand.getSelectedRow();
                if (data.length == 0) return;
                ResultSet result = db.query(
                        "select * from offer where offer like ? and area >= ? and price <= ? " +
                                "and type in (select distinct type from demand_type where did = ?) " +
                                "and decor in (select distinct decor from demand_decor where did = ?) " +
                                "and direction in (select distinct direction from demand_direction where did = ?)",
                        new Object[] {
                                (data[1].equals("求租")?"出租":"出售"),
                                data[5], data[9], data[0], data[0], data[0]
                        }
                );
                panelResult.remove(demandResult);
                panelResult.remove(offerResult);
                offerResult = new DataTable(new entity.Offer(result));
                panelResult.add(offerResult);
                panelResult.add(demandResult);
                panelResult.updateUI();
            }
            catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        });
    }

    public static void main(String[] args) {
        new Match().setVisible(true);
    }
}
